home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / utilities / misc / show1.0.lha / show / RCS / misc.c,v < prev    next >
Encoding:
Text File  |  1994-12-07  |  7.7 KB  |  334 lines

  1. head    1.82;
  2. access;
  3. symbols
  4.     fontscreen:1.81
  5.     good:1.81;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.82
  11. date    94.12.04.23.36.40;    author jsshephe;    state Exp;
  12. branches;
  13. next    ;
  14.  
  15.  
  16. desc
  17. @original
  18. @
  19.  
  20.  
  21. 1.82
  22. log
  23. @included libraries/gadtools.h
  24. @
  25. text
  26. @/* Files for reading and writing the preferences file */
  27. /* $Id: misc.c,v 1.81 1994/12/04 02:09:07 jsshephe Exp jsshephe $
  28.  * $Revision: 1.81 $
  29.  * $Log: misc.c,v $
  30.  * Revision 1.81  1994/12/04  02:09:07    jsshephe
  31.  * Conditionally compiled some debugging text.
  32.  *
  33.  * Revision 1.8  1994/11/27  22:15:40  jsshephe
  34.  * Changed Error() as to not print "Error:" on the first line.
  35.  *
  36.  * Revision 1.7  1994/11/27  21:44:56  jsshephe
  37.  * Since the title is always the same, only passed error message to Error().
  38.  *
  39.  * Revision 1.6  1994/11/27  04:01:16  jsshephe
  40.  * Fixed a bug in SearchSuffix() where it searched past the end of the list.
  41.  *
  42.  * Revision 1.5  1994/08/19  19:38:54  jsshephe
  43.  * changed the filename from loadsave.c to misc.c
  44.  * Added SearchSuffix() and strrstr()
  45.  *
  46.  * Revision 1.4  1994/08/18  23:19:57  jsshephe
  47.  * Put up a error requester when it could not open the prefs file
  48.  *
  49.  * Revision 1.3  1994/08/18  21:24:27  jsshephe
  50.  * changed the prefs structure to use variable length strings
  51.  *
  52.  * Revision 1.2  1994/08/18  20:58:30  jsshephe
  53.  * Added Error()
  54.  * fixed a few things
  55.  *
  56.  * Revision 1.1  1994/08/18  06:10:50  jsshephe
  57.  * Initial revision
  58.  */
  59.  
  60. #include <exec/types.h>
  61. #include <exec/lists.h>
  62. #include <exec/memory.h>
  63. #include <libraries/iffparse.h>
  64. #include <libraries/gadtools.h>
  65. #include <dos/dos.h>
  66. #include <string.h>
  67. #include <stdlib.h>
  68. #include <stdio.h>
  69.  
  70. /* protos */
  71. #ifdef __OPTIMIZE__
  72. #include <inline/exec.h>
  73. #include <inline/intuition.h>
  74. #include <inline/dos.h>
  75. #else
  76. #include <inline/stubs.h> /* shut up */
  77. #include <clib/exec_protos.h>
  78. #include <clib/intuition_protos.h>
  79. #include <clib/dos_protos.h>
  80. #endif
  81. #include <inline/iffparse.h>
  82. #include "showprefs.h"
  83.  
  84. /* Save preference file as 'filename'
  85.  * filename is an absolute path
  86.  */
  87. void SavePrefs(char *filename) {
  88.     struct IFFHandle *iff= NULL;
  89.     struct SuffixList *SufList;
  90.  
  91.     /* struct SuffixNode *SufNode */
  92.  
  93.     if (! (iff = AllocIFF())) {
  94.     Error("AllocIFF() failed");
  95.     goto Cleanup;
  96.     }
  97.  
  98.     if    ( !(iff->iff_Stream = Open(filename,MODE_NEWFILE))) {
  99.     Error("Open() failed");
  100.     goto Cleanup;
  101.     }
  102.  
  103.     InitIFFasDOS(iff);
  104.  
  105.     if (OpenIFF(iff,IFFF_WRITE)) {
  106.     Error("OpenIFF() failed.");
  107.     goto Cleanup;
  108.     }
  109.  
  110.     PushChunk(iff,ID_SHOW,ID_FORM,IFFSIZE_UNKNOWN);
  111.     for (SufList = (struct SuffixList *)Suffix_List->lh_Head;
  112.      SufList->sl_Node.ln_Succ;
  113.      SufList= (struct SuffixList *)SufList->sl_Node.ln_Succ) {
  114.  
  115.     UWORD len;
  116.  
  117.     PushChunk(iff,ID_SHOW,ID_PREF,IFFSIZE_UNKNOWN);
  118.  
  119.     /* new way - write out the length of the string
  120.      *         then the string itself (NULL-terminated)
  121.      */
  122.     len = strlen(SufList->sl_Node.ln_Name)+1;
  123.     WriteChunkBytes(iff,&len,sizeof(UWORD));
  124.     WriteChunkBytes(iff,SufList->sl_Node.ln_Name,len);
  125.  
  126.     len = strlen(SufList->command)+1;
  127.     WriteChunkBytes(iff,&len,sizeof(UWORD));
  128.     WriteChunkBytes(iff,SufList->command,len);
  129.  
  130.     WriteChunkBytes(iff,&SufList->Asynch,sizeof(BOOL));
  131.  
  132.     PopChunk(iff);
  133.     }
  134.     PopChunk(iff);
  135.  
  136. Cleanup :
  137.     if (iff) {
  138.     CloseIFF(iff);
  139.     if (iff->iff_Stream)
  140.         Close(iff->iff_Stream);
  141.     FreeIFF(iff);
  142.     }
  143. }
  144.  
  145. /* Load Preference file 'filename' */
  146. /* Dup - TRUE means save the settings in the duplicate Array */
  147. /*     - used for restore */
  148. /* Duplicate is only used if Dup is TRUE */
  149. void Load(char *filename, BOOL Dup,struct List *Duplicate) {
  150.     struct IFFHandle *iff= NULL;
  151.     struct SuffixNode SufNode;
  152.     struct ContextNode *cn;
  153.     int error;
  154.  
  155.     if (! (iff = AllocIFF())) {
  156.     Error("AllocIFF() failed");
  157.     goto LoadCleanup;
  158.     }
  159.  
  160.     if    ( !(iff->iff_Stream = Open(filename,MODE_OLDFILE))) {
  161.     Error("Cannot open env:show.prefs");
  162.     goto LoadCleanup;
  163.     }
  164.  
  165.     InitIFFasDOS(iff);
  166.  
  167.     if (error=OpenIFF(iff,IFFF_READ)) {
  168.     char errormsg[MAX_LENGTH];
  169.     sprintf(errormsg,"OpenIFF() failed. Error: %d",error);
  170.     Error(errormsg);
  171.     goto LoadCleanup;
  172.     }
  173.  
  174.     /* stop at PREFS hunks */
  175.     StopChunk(iff,ID_SHOW,ID_PREF);
  176.  
  177.     for(;;) {
  178.     error=ParseIFF(iff,IFFPARSE_SCAN);
  179.  
  180.     if (error == IFFERR_EOC) continue;
  181.     else if (error) break;
  182.  
  183.     cn = CurrentChunk(iff);
  184.  
  185.     if ( cn && (cn->cn_Type == ID_SHOW) && (cn->cn_ID == ID_PREF)) {
  186.         struct SuffixList *NewNode;
  187.         UWORD len;
  188.  
  189.         /* new way - uses variable length strings */
  190.         ReadChunkBytes(iff,&len,sizeof(UWORD));
  191.         ReadChunkBytes(iff,SufNode.suffix,len);
  192.  
  193.         ReadChunkBytes(iff,&len,sizeof(UWORD));
  194.         ReadChunkBytes(iff,SufNode.command,len);
  195.  
  196.         ReadChunkBytes(iff,&SufNode.Asynch,sizeof(BOOL));
  197.  
  198.         if (NewNode =MakeNode(SufNode.suffix,SufNode.command,SufNode.Asynch)) {
  199.         AddTail(Suffix_List,(struct Node *)NewNode);
  200.         }
  201.         else {
  202.         error = IFFERR_NOMEM;
  203.         break;
  204.         }
  205.  
  206.         /* Duplicate the list if we need to */
  207.         if (Dup) {
  208.         struct SuffixList *DupNode;
  209.         if (DupNode =MakeNode(SufNode.suffix,SufNode.command,SufNode.Asynch))
  210.             AddTail(Duplicate,(struct Node *)DupNode);
  211.         else {
  212.             error = IFFERR_NOMEM;
  213.             break;
  214.         }
  215.         }
  216.  
  217.     }
  218.     }
  219.  
  220.     if (error && (error != IFFERR_EOF)) {
  221.     char errormsg[MAX_LENGTH];
  222.     sprintf(errormsg,"IFF read failed. Error %d",error);
  223.     Error(errormsg);
  224.     }
  225.  
  226. LoadCleanup :
  227.     if (iff) {
  228.     CloseIFF(iff);
  229.     if (iff->iff_Stream)
  230.         Close(iff->iff_Stream);
  231.     FreeIFF(iff);
  232.     }
  233. }
  234.  
  235.  
  236. /* Make a new SuffixList Node with the specified parameters */
  237. /* returns NULL is something did not succeed */
  238. /* else returns the new node */
  239. struct SuffixList *MakeNode(char *suffix, char *command, BOOL Asynch) {
  240.     struct SuffixList *NewNode = AllocMem(sizeof(struct SuffixList),MEMF_PUBLIC|MEMF_CLEAR);
  241.     if (NewNode) {
  242.     NewNode->sl_Node.ln_Name = AllocMem(MAX_LENGTH*sizeof(char),MEMF_CLEAR|MEMF_PUBLIC);
  243.     if (NewNode->sl_Node.ln_Name) {
  244.         strcpy(NewNode->sl_Node.ln_Name,suffix);
  245.         NewNode->sl_Node.ln_Pri = 0;
  246.         NewNode->sl_Node.ln_Type = NT_USER;
  247.         strcpy(NewNode->command,command);
  248.         NewNode->Asynch = Asynch;
  249.     }
  250.     else {
  251.         FreeMem(NewNode,sizeof(struct SuffixList));
  252.         NewNode = NULL;
  253.     }
  254.    }
  255.    return NewNode;
  256. }
  257.  
  258. /* free up nodes in list */
  259. /* does not free list */
  260. void Destroy_List(struct List *LList) {
  261.     struct SuffixList *oldNode;
  262.     while (oldNode=(struct SuffixList *)RemHead(LList)) {
  263.     if (oldNode->sl_Node.ln_Name)
  264.         FreeMem(oldNode->sl_Node.ln_Name,MAX_LENGTH*sizeof(char));
  265.     FreeMem(oldNode, sizeof(struct SuffixList));
  266.     }
  267. }
  268.  
  269. /* display error requester with the respective title and error message */
  270. void Error(char *error) {
  271.     static char title[] = "Fatal Error";
  272.  
  273.     struct EasyStruct es = {
  274.     sizeof(struct EasyStruct),
  275.     NULL,
  276.     title,
  277.     error,
  278.     "OK"
  279.     };
  280.     EasyRequest(NULL,&es,NULL,NULL);
  281. }
  282.  
  283.  
  284. /* returns the Node with the suffix defined in filename */
  285. struct Node *SearchSuffix(struct List *SufList, char *filename) {
  286.     struct Node *retval;
  287.  
  288.     #ifdef debug
  289.     printf("SearchSuffix filename to be searched: %s\n",filename);
  290.     #endif
  291.  
  292.     /* make sure the list is not empty */
  293.     if (SufList->lh_TailPred != (struct Node *)SufList) {
  294.     for(retval = SufList->lh_Head;retval->ln_Succ &&
  295.       !strrstr(filename,retval->ln_Name); retval =retval->ln_Succ) {
  296.         #ifdef debug
  297.         printf("Name %s\n",retval->ln_Name);
  298.         #endif
  299.     } /* for */
  300.  
  301.     if (retval && retval->ln_Succ && strrstr(filename,retval->ln_Name)) {
  302.         #ifdef debug
  303.         puts("Found");
  304.         #endif
  305.         return retval;
  306.     }
  307.     else {
  308.         #ifdef debug
  309.         puts("Not Found");
  310.         #endif
  311.         return NULL;
  312.     }
  313.     }
  314.     else
  315.     return NULL;
  316. }
  317.  
  318. /* finds last occurrence of substr in string */
  319. char *strrstr(char *string, const char *substr) {
  320.     char *tempstring = string;
  321.     char *found = NULL;
  322.     char *temp;
  323.  
  324.     if (string) {
  325.     while (temp = strstr(tempstring,substr)) {
  326.         tempstring = temp;
  327.         tempstring++;
  328.         found = temp;
  329.     }
  330.     }
  331.     return found;
  332. }
  333. @
  334.